﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;
using BLL;

namespace CuaHangBangDia.QLKHThue
{
    public partial class ThemKhachHangThueForm : Form
    {
        public ThemKhachHangThueForm()
        {
            InitializeComponent();
        }
        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        private KhachHangThue GetData(int i)
        {
            KhachHangThue kh = new KhachHangThue();
            kh.TenKhachHang = txtTenKH.Text;
            kh.Tuoi = Convert.ToInt32(txtTuoi.Text);
            kh.DiaChi = txtDiaChi.Text;
            kh.NgayThue = Convert.ToDateTime(dtpNgayThue.Value.ToShortDateString());
            return kh;
        }
        private ChiTietKhachHangThue GetDataChiTiet(int i)
        {
            ChiTietKhachHangThue kh = new ChiTietKhachHangThue();
            kh.TienCoc = Convert.ToDouble(txtTienCoc.Text);
            kh.GhiChu = txtGhiChu.Text;
            kh.SoLuong = Convert.ToInt32(dgvThemKHTHue.Rows[i].Cells[2].Value.ToString());
            kh.TongTien = Convert.ToDouble(lbTinhTongTien.Text);
            return kh;
        }
        private void btnThem_Click_1(object sender, EventArgs e)
        {
            try
            {
                int makh = 0;
                int mabd = 0;
                KhachHangThue kh = GetData(makh);
                if (KhachHangThue.Objects.Add(kh) > 0)
                {
                    for (int i = 0; i < dgvThemKHTHue.Rows.Count - 1; i++)
                    {
                        ChiTietKhachHangThue khct = new ChiTietKhachHangThue();
                        DateTime t = kh.NgayThue;
                        List<KhachHangThue> l = KhachHangThue.Objects.Find(m => m.NgayThue == t).ToList();
                        foreach (KhachHangThue item in l)
                        {
                            makh = item.MaKhachHang;

                        }
                        string ten = dgvThemKHTHue.Rows[i].Cells[0].Value.ToString();
                        List<BangDia> lbd = BangDia.Objects.Find(bd => bd.TenBangDia == ten).ToList();
                        foreach (BangDia item in lbd)
                        {
                            mabd = item.MaBangDia;
                        }
                        khct.MaKhachHang = makh;
                        khct.MaBangDia = mabd;
                        khct.SoLuong = Convert.ToInt32(dgvThemKHTHue.Rows[i].Cells[2].Value);
                        khct.GhiChu = txtGhiChu.Text;
                        khct.TienCoc = Convert.ToDouble(txtTienCoc.Text);
                        khct.TongTien = Convert.ToDouble(dgvThemKHTHue.Rows[i].Cells[3].Value);
                        ChiTietKhachHangThue.Objects.Insert(khct);

                        string tenbd = dgvThemKHTHue.Rows[i].Cells[0].Value.ToString();
                        List<BangDia> bangdia = BangDia.Objects.Find(k => k.TenBangDia == tenbd).ToList();
                        foreach (BangDia item in bangdia)
                        {
                            BangDia bds = new BangDia();
                            bds.MaBangDia = item.MaBangDia;
                            bds.TenBangDia = item.TenBangDia;
                            bds.MaLoai = item.MaLoai;
                            bds.MaTheLoai = item.MaTheLoai;
                            bds.Gia = item.Gia;
                            int sl = Convert.ToInt32(dgvThemKHTHue.Rows[i].Cells[2].Value);
                            bds.SoLuong = item.SoLuong - sl;
                            BangDia.Objects.Update(bds);
                        }
                    }
                    MessageBox.Show("Thêm Thành Công !", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    DateTime d = dtpNgayThue.Value;
                    List<BanHang> lk = BanHang.Objects.Find(m => (m.NgayBan.Day == d.Day && m.NgayBan.Month == d.Month && m.NgayBan.Year == d.Year)).ToList();
                    if (lk.Count == 0)
                    {
                        BanHang g = new BanHang();
                        g.NgayBan = d;
                        g.TienBan = 0;
                        g.TienChoThue = Convert.ToDouble(lbTinhTongTien.Text);
                        g.KhacChi = 0;
                        g.KhacThu = 0;
                        g.Tong = Convert.ToDouble(lbTinhTongTien.Text);
                        BanHang.Objects.Insert(g);
                    }
                    else
                    {
                        BanHang bb = new BanHang();
                        foreach (BanHang item in lk)
                        {
                            bb.NgayBan = item.NgayBan;
                            bb.TienBan = item.TienBan;
                            bb.TienChoThue = item.TienChoThue + Convert.ToDouble(lbTinhTongTien.Text);
                            bb.KhacChi = item.KhacChi;
                            bb.KhacThu = item.KhacThu;
                            bb.Tong = item.Tong + Convert.ToDouble(lbTinhTongTien.Text);
                        }
                        BanHang.Objects.Update(bb);
                    }
                    Reset();
                }
                else
                {
                    MessageBox.Show("Thêm Lỗi !", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (System.Exception)
            {
                MessageBox.Show("Lỗi Thêm Dữ Liệu  !", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void Reset()
        {
            txtTenKH.Text = "";
            txtDiaChi.Text = "";
            txtGhiChu.Text = "";
            txtTienCoc.Text = "";
            txtTuoi.Text = "";
            lbTinhTongTien.Text = "Tổng Tiền Thuê";
            dtpNgayThue.Value = DateTime.Now;
            dgvThemKHTHue.Rows.Clear();
        }
        private void btnNhapLai_Click(object sender, EventArgs e)
        {
            Reset();
        }

        private void btnDong_Click_1(object sender, EventArgs e)
        {
            this.Dispose();
        }
        private void dgvThemKHTHue_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            DataGridViewRow r = dgvThemKHTHue.CurrentRow;
            TextBox txt = e.Control as TextBox;
            if (e.Control is TextBox && r.Cells[0].Selected)
            {
                for (int i = 0; i < dgvThemKHTHue.Rows.Count; i++)
                {
                    txt.Multiline = false;
                    txt.AutoCompleteCustomSource = new AutoCompleteStringCollection();
                    List<BangDia> li = BangDia.Objects.GetAll().ToList();
                    foreach (BangDia item in li)
                    {
                        txt.AutoCompleteCustomSource.Add(item.TenBangDia);
                    }
                    txt.AutoCompleteMode = AutoCompleteMode.Suggest;
                    txt.AutoCompleteSource = AutoCompleteSource.CustomSource;
                }
            }
            else
            {
                txt.AutoCompleteMode = AutoCompleteMode.None;
                txt.AutoCompleteSource = AutoCompleteSource.None;
            }
        }
        private void dgvThemKHTHue_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            double dem = 0;
            for (int i = 0; i < dgvThemKHTHue.Rows.Count - 1; i++)
            {
                DataGridViewRow r = dgvThemKHTHue.CurrentRow;
                if (dgvThemKHTHue.Rows[i].Cells[0].Value != null)
                {
                    List<BangDia> j = BangDia.Objects.Find(p => p.TenBangDia ==
                    dgvThemKHTHue.Rows[i].Cells[0].Value.ToString()).ToList();
                    foreach (BangDia b in j)
                    {
                        dgvThemKHTHue.Rows[i].Cells[1].Value = b.Gia.ToString();
                    }

                }
                double dg = Convert.ToDouble(dgvThemKHTHue.Rows[i].Cells[1].Value);
                int sl = Convert.ToInt32(dgvThemKHTHue.Rows[i].Cells[2].Value);
                dgvThemKHTHue.Rows[i].Cells[3].Value = ((dg * sl) * 45) / 100;
                dem += Convert.ToDouble(dgvThemKHTHue.Rows[i].Cells[3].Value);
                lbTinhTongTien.Text = dem.ToString();
            }
        }

        private void txtTenKH_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString().IndexOfAny(@"0123456789!@#$%^&*()_+=|\{}[]?><.,';:".ToCharArray()) != -1)
            {
                e.Handled = true;
            }

            else
            {
                e.Handled = false;
            }
        }

        private void txtDiaChi_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString().IndexOfAny(@"0123456789!@#$%^&*()_+=|\{}[]?><.,';:".ToCharArray()) != -1)
            {
                e.Handled = true;
            }

            else
            {
                e.Handled = false;
            }
        }

        private void txtTuoi_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString().IndexOfAny(@"qwertyuioplkjhgfdsazxcvbnm!@#$%^&*()_+=|\{}[]?><.,';:".ToCharArray()) != -1)
            {
                e.Handled = true;
            }

            else
            {
                e.Handled = false;
            }
        }

        private void txtTienCoc_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString().IndexOfAny(@"qwertyuioplkjhgfdsazxcvbnm!@#$%^&*()_+=|\{}[]?><.,';:".ToCharArray()) != -1)
            {
                e.Handled = true;
            }

            else
            {
                e.Handled = false;
            }
        }
        private bool KT(DataGridViewCellValidatingEventArgs e)
        {
            if (dgvThemKHTHue.Columns[e.ColumnIndex].Name == "SLThueColumn")
                return true;
            return false;
        }
        private void dgvThemKHTHue_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            double newInteger;
            if (KT(e))
            {
                if ((!double.TryParse(e.FormattedValue.ToString(), out newInteger) && 
                    !String.IsNullOrWhiteSpace(e.FormattedValue.ToString()) || newInteger <= 0 ))
                {
                    dgvThemKHTHue.Rows[e.RowIndex].ErrorText = "Số Lượng là lớn hơn 0,và khác chuỗi";
                    e.Cancel = true;
                }
            }

        }

        private void dgvThemKHTHue_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            dgvThemKHTHue.Rows[e.RowIndex].ErrorText = "";
        }


    }
}